import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        orderId: "", //订单id
        orderLists: [],
        integralDetailData: {},

        orderData: {},
        orderType: "",
        show: false,
        show2: false,
        show3: false,
        orderIdTotal: "",
        couponPrice: 0,
        orderNumber: "", //订单编号
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        this.setData({
            orderId: options.id,
        });
        // this.orderDetailsPsot();
    },
    async orderDetailsPsot() {
        let res = await request({
            url: "BlanketOrder/findByIdUserOrder",
            data: {
                orderId: this.data.orderId,
            },
        });
        console.log(res);

        if (res.code == 200) {
            this.setData({
                orderNumber: res.data.orderNumber,
                orderData: res.data,
                orderType: res.data.orderType,
                orderLists: res.data.orderGoodsList,
            });
            if (res.data.couponPrice == null) {
                this.setData({
                    couponPrice: 0,
                });
            } else {
                this.setData({
                    couponPrice: res.data.couponPrice,
                });
            }
        }
        console.log(this.data.orderLists);
    },
    //取消订单
    async cancellation(e) {
        console.log(e);
        this.setData({
            show: true,
            orderIdTotal: e.currentTarget.dataset.id,
        });
    },
    onClose() {
        this.setData({
            show: false,
        });
    },
    //取消订单的确认按钮
    async getUserInfo() {
        let res = await request({
            url: "BlanketOrder/cancelUserOrder",
            data: { orderId: this.data.orderIdTotal },
        });
        console.log(res);
        if (res.code == 200) {
            wx.redirectTo({
                url: "../my_order/my_order",
            });
        }
    },
    // 订单支付
    async payOrder() {
        let res = await request({
            url: "pay/wx/toPay",
            data: { orderNumber: this.data.orderNumber },
        });
        console.log(res);
        if (res.code == 200) {
            let aa = res.data.timeStamp + "";
            console.log(aa);
            wx.requestPayment({
                timeStamp: aa, //时间戳
                nonceStr: res.data.nonceStr, //随机字符串
                package: res.data.package, //统一下单接口返回的 prepay_id 参数值
                signType: res.data.signType, //签名算法
                paySign: res.data.sign, //签名
                success: function(res) {
                    console.log(res, "支付成功");
                    // wx.redirectTo({
                    //     url: '',
                    // });
                },
                fail: function(err) {
                    console.log(err, "支付失败");
                },
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
        }
    },
    //删除订单
    deleteOrder(e) {
        this.setData({
            show2: true,
            orderIdTotal: e.currentTarget.dataset.id,
        });
    },
    onCloseDelete() {
        this.setData({
            show2: false,
        });
    },
    //删除确认按钮
    async getUserInfoDelete() {
        let res = await request({
            url: "BlanketOrder/deleteUserOrder",
            data: {
                orderId: this.data.orderIdTotal,
            },
        });
        console.log(res);
        if (res.code == 200) {
            wx.redirectTo({
                url: "../my_order/my_order",
            });
        }
    },

    // 确认收货
    confirmReceip(e) {
        this.setData({
            show3: true,
            orderIdTotal: e.currentTarget.dataset.id,
        });
    },
    onCloseTake() {
        this.setData({
            show3: false,
        });
    },
    async getUserInfoTake() {
        let res = await request({
            url: "BlanketOrder/update",
            data: {
                orderId: this.data.orderIdTotal,
                orderType: 3,
            },
        });
        console.log(res);
        if (res.code == 200) {
            wx.redirectTo({
                url: "../my_order/my_order",
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.orderDetailsPsot();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});